package com.matrix.editor

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.Spannable
import android.text.SpannableString
import android.text.TextWatcher
import android.text.style.BackgroundColorSpan
import android.util.Log
import android.view.View
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.matrix.editor.bean.InputTextData
import com.matrix.editor.databinding.ActivityEditorTextInputBinding
import com.matrix.editor.view.ColorSeekBar

class EditorTextInputActivity : AppCompatActivity() {
    private val mResultCode = 301
    private var mTextColor = 0// 需要回传的文字颜色
    private var mBgColor = 0//需要回传的背景色
    private var mTextInputId: String? = null
    private var Deputytext: TextView? = null
    internal var isChange = false//当前编辑模式 默认为true，为编辑字体颜色(此时关闭背景色)，false为编辑背景颜色
    lateinit var mViewBinding: ActivityEditorTextInputBinding

    companion object {
        private val EXTRA_CODE = "extraInput"
        fun intent(context: Context, data: InputTextData?): Intent {
            val intent = Intent(context, EditorTextInputActivity::class.java)
            intent.putExtra(EXTRA_CODE, data)
            return intent
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewBinding()
        setContentView(mViewBinding.root)
        Deputytext = mViewBinding.deputyText
        AdjustResizeInFullScreen.assistActivity(this)
        initData()
        initListener()
    }

    private fun initViewBinding() {
        mViewBinding=ActivityEditorTextInputBinding.inflate(layoutInflater)
    }


    private fun initData() {
        val readyData = intent.getSerializableExtra(EXTRA_CODE) as? InputTextData
        readyData?.let {
            mTextInputId = readyData.id
            mViewBinding.etInput.setText(readyData.text ?: "")
            mViewBinding.colorBarInput.setOnInitDoneListener(object : ColorSeekBar.OnInitDoneListener {  //用于线条型回调
                override fun done() {
                    var position = 8
                    readyData.color?.let {
                        position = mViewBinding.colorBarInput.getColorIndexPosition(it)
                    }
                    mViewBinding.colorBarInput.setColorBarPosition(position)
                }
            })
            mViewBinding.cgColors.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
                override fun onCheckedChanged(p0: RadioGroup?, p1: Int) {
                    var position = 8
                    readyData.color?.let {
                        position = mViewBinding.cgColors.checkColor
                    }
                }
            })
        }
    }

    private fun initListener() {
        mViewBinding.tvCancelInput.setOnClickListener {
            finish()
        }
        mViewBinding.tvConfirmInput.setOnClickListener {
            val text = mViewBinding.etInput.text.trim()
            if (text.isBlank()) {
                finish()
            } else {
                val intent = Intent()
                if (mTextColor != 0) {
                    intent.putExtra(mResultCode.toString(),InputTextData(mTextInputId, text.toString(), mTextColor,mBgColor))
                } else {
                    intent.putExtra(mResultCode.toString(), InputTextData(mTextInputId, text.toString(),resources.getColor(R.color.image_color_white),mBgColor))
                }
                setResult(mResultCode, intent)
                finish()
            }
        }

        mViewBinding.etInput!!.addTextChangedListener(object : TextWatcher {

            private var temp: CharSequence? = null

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                temp = s
            }

            override fun afterTextChanged(s: Editable) {
                val spanString = SpannableString(temp!!.toString())
                val span = BackgroundColorSpan(mViewBinding.cgColors.checkColor)
                spanString.setSpan(span, 0, temp!!.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                Deputytext!!.text = spanString
            }
        })
        mViewBinding.tvbackgroundchange.setOnClickListener {
            if (!isChange) {
                isChange = true
                Deputytext!!.visibility = View.VISIBLE
                Deputytext!!.setTextColor(resources.getColor(R.color.image_color_white))
                val spanString = SpannableString(mViewBinding.etInput.editableText!!.toString())
                val span = BackgroundColorSpan(mViewBinding.cgColors.checkColor)
                spanString.setSpan(span, 0, mViewBinding.etInput.editableText!!.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                Deputytext!!.text = spanString
                mViewBinding.tvbackgroundchange.setImageDrawable(resources.getDrawable(R.drawable.font_bottom_ic))
                if (mTextColor == 0) {
                    mViewBinding.etInput.setTextColor(Color.parseColor("#26252a"))
                }
//        else if (mViewBinding.cgColors.checkColor == mTextColor) {
//          mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
//        }
                else if (mViewBinding.cgColors.checkColor == Color.parseColor("#F9F9F9")) {
                    mViewBinding.etInput.setTextColor(Color.parseColor("#26252a"))
                } else {
                    mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
                }
            } else {
                isChange = false
                Deputytext!!.visibility = View.GONE
                mViewBinding.etInput!!.setTextColor(mViewBinding.cgColors.checkColor)
                Log.e("mViewBinding.cgColors.checkColor==", "isChange==true" + mViewBinding.cgColors.checkColor)
                mViewBinding.tvbackgroundchange.setImageDrawable(resources.getDrawable(R.drawable.font_bottom_ic_select))
            }
        }
        mViewBinding.cgColors.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(p0: RadioGroup?, p1: Int) {
                if (isChange) {
//          if (mViewBinding.cgColors.checkColor == mTextColor) {
//            mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
//            mTextColor = Color.parseColor("#F9F9F9")
//          } else if (Color.parseColor("#F9F9F9") == mViewBinding.cgColors.checkColor) {
//            mViewBinding.etInput.setTextColor(Color.parseColor("#26252a"))
//            mTextColor = Color.parseColor("#26252a")
//          } else if (Color.parseColor("#26252a") == mViewBinding.cgColors.checkColor) {
//            mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
//            mTextColor = Color.parseColor("#F9F9F9")
//          }else{
//
//          }
                    if (mTextColor == 0) {
                        mViewBinding.etInput.setTextColor(Color.parseColor("#26252a"))
                        mTextColor = Color.parseColor("#F9F9F9")
                    }
//        else if (mViewBinding.cgColors.checkColor == mTextColor) {
//          mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
//        }
                    else if (mViewBinding.cgColors.checkColor == Color.parseColor("#F9F9F9")) {
                        mViewBinding.etInput.setTextColor(Color.parseColor("#26252a"))
                        mTextColor = Color.parseColor("#26252a")
                    } else {
                        mViewBinding.etInput.setTextColor(Color.parseColor("#F9F9F9"))
                        mTextColor = Color.parseColor("#F9F9F9")
                    }

                    val spanString = SpannableString(mViewBinding.etInput.editableText!!.toString())
                    val span = BackgroundColorSpan(mViewBinding.cgColors.checkColor)
                    mBgColor = mViewBinding.cgColors.checkColor
                    spanString.setSpan(span, 0, mViewBinding.etInput.editableText!!.length,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    Deputytext!!.text = spanString
                    mBgColor = mViewBinding.cgColors.checkColor
                } else {
                    mViewBinding.etInput.setTextColor(mViewBinding.cgColors.checkColor)
                    mTextColor = mViewBinding.cgColors.checkColor
                }
                Log.e("mViewBinding.cgColors.checkColor==", "mViewBinding.cgColors" + mViewBinding.cgColors.checkColor)
            }
        })

    }


    override fun finish() {
        super.finish()
        overridePendingTransition(0, R.anim.animation_top_to_bottom)
    }
}